package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.MoneyWithdrawService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.MoneyWithdrawDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.po.MoneyWithdraw;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 谢洛涛
 * @date 2021/4/8 9:17
 */
@RestController
@Slf4j
@RequestMapping("/api/moneyWithdraw")
public class MoneyWithdrawController extends BaseController {
    @Autowired
    private MoneyWithdrawService moneyWithdrawService;
    @Autowired
    private UserService userService;
    @Autowired
    private ErrorLogCommonService errorLogCommonService;
    @PostMapping("/list")
    public ServerResponse findMoneyWithdrawList(HttpServletRequest request, @RequestBody  MoneyWithdraw moneyWithdraw){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String keyword = request.getParameter("keyword");
        UserInfo userInfo = userService.getLoginUserInfo(request);
        moneyWithdraw.setUserInfo(userInfo);
        moneyWithdraw.setPageNum(Integer.parseInt(pageNum));
        moneyWithdraw.setPageSize(Integer.parseInt(pageSize));
        moneyWithdraw.setKeyword(keyword);
        return ServerResponse.createBySuccess(moneyWithdrawService.findMoneyWithdrawList(moneyWithdraw));
    }

    /**提现统计**/
    @PostMapping("/findMoneyWithdrawStatistics")
    public ServerResponse findMoneyWithdrawStatistics(HttpServletRequest request, @RequestBody  MoneyWithdraw moneyWithdraw){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        moneyWithdraw.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(moneyWithdrawService.findMoneyWithdrawStatistics(moneyWithdraw));
    }


    /**申请提现**/
    @PostMapping
    public ServerResponse AddMoneyWithdraw(HttpServletRequest request,@RequestBody MoneyWithdrawDto moneyWithdrawDto) throws Exception{
        UserInfo userInfo = userService.getLoginUserInfo(request);
        moneyWithdrawDto.setUserInfo(userInfo);
        return moneyWithdrawService.AddMoneyWithdraw(moneyWithdrawDto);
    }
    /**签收发票*/
    @PutMapping("/updateSignInInvoice")
    public ServerResponse signInInvoice(@RequestBody MoneyWithdraw moneyWithdraw) {
        return ServerResponse.createBySuccess(moneyWithdrawService.updateSignInInvoice(moneyWithdraw));

    }


    /**处理提现状态（处理中，拒绝，手动打款）**/
    @PutMapping
    public ServerResponse updateMoneyWithdraw(HttpServletRequest request,@RequestBody List<MoneyWithdrawDto> moneyWithdrawDto) throws Exception{
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(!(RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()))){
            return  ServerResponse.createByErrorMessage("不是管理员没权限处理提现申请");
        }
        return moneyWithdrawService.updateMoneyWithdraw(moneyWithdrawDto);
    }
    /**代付提现*/
    @PutMapping("/daiFuBalance")
    public ServerResponse daiFuBalance(HttpServletRequest request,@RequestBody List<MoneyWithdrawDto> moneyWithdrawDto) throws Exception{
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if(!(RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()))){
            return  ServerResponse.createByErrorMessage("不是管理员没权限处理提现申请");
        }
        return moneyWithdrawService.daiFuBalance(moneyWithdrawDto,userInfo);
    }

    @PostMapping("/daiFuNotice")
    public String daiFuNotice(HttpServletRequest request ) {
        try {
            return moneyWithdrawService.daiFuNotice(request);
        } catch (Exception e) {
            errorLogCommonService.writerInfoException("提现扣除用户余额失败",null,e);
            return "error";
        }
    }

}
